home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / ButtonGroup.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  123 lines

  1. /*
  2.  * @(#)ButtonGroup.java    1.18 98/02/27
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.event.*;
  23. import java.util.Vector;
  24. import java.util.Enumeration;
  25. import java.io.Serializable;
  26.  
  27. /**
  28.  * This class is used to create a multiple-exclusion scope for
  29.  * a set of buttons. Creating a set of buttons with the
  30.  * same ButtonGroup object means that turning "on" on of those buttons 
  31.  * turns off all other buttons in the group. A ButtonGroup can be used with
  32.  * sets of JButton, JRadioButton, or JRadioButtonMenuItem objects.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate 
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.18 02/27/98
  42.  * @author Jeff Dinkins
  43.  */
  44. public class ButtonGroup implements Serializable {
  45.  
  46.     // the list of buttons participating in this group
  47.     protected Vector buttons = new Vector();
  48.  
  49.     /**
  50.      * The current choice.
  51.      */
  52.     ButtonModel selection = null;
  53.  
  54.     /**
  55.      * Creates a new ButtonGroup.
  56.      */
  57.     public ButtonGroup() {}
  58.  
  59.     /**
  60.      * Adds the button to the group.
  61.      */ 
  62.     public void add(AbstractButton b) {
  63.         if(b == null) {
  64.             return;
  65.         }
  66.         buttons.addElement(b);
  67.         if(selection == null && b.isSelected()) {
  68.             selection = b.getModel();
  69.         }
  70.         b.getModel().setGroup(this);
  71.     }
  72.  
  73.     /**
  74.      * Removes the button from the group.
  75.      */ 
  76.     public void remove(AbstractButton b) {
  77.         if(b == null) {
  78.             return;
  79.         }
  80.         buttons.removeElement(b);
  81.         if(b.getModel() == selection) {
  82.             selection = null;
  83.         }
  84.         b.getModel().setGroup(null);
  85.     }
  86.  
  87.     /**
  88.      * Return all the buttons that are participating in
  89.      * this group.
  90.      */
  91.     public Enumeration getElements() {
  92.         return buttons.elements();
  93.     }
  94.  
  95.     /**
  96.      * Return the selected button model.
  97.      */
  98.     public ButtonModel getSelection() {
  99.         return selection;
  100.     }
  101.  
  102.     /**
  103.      * Sets the selected value for the button.
  104.      */
  105.     public void setSelected(ButtonModel m, boolean b) {
  106.         if(b && m != selection) {
  107.             ButtonModel oldSelection = selection;
  108.             selection = m;
  109.             if(oldSelection != null) {
  110.                 oldSelection.setSelected(false);
  111.             }
  112.         } 
  113.     }
  114.  
  115.     /**
  116.      * Returns the selected value for the button.
  117.      */
  118.     public boolean isSelected(ButtonModel m) {
  119.         return (m == selection);
  120.     }
  121.  
  122. }
  123.